package xcmg.device.web.agent;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.dao.entity.repair.RepairDO;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceBorrowOutCompose;
import xcmg.device.service.agent.ServiceBorrowOutService;
import xcmg.device.service.repair.RepairService;
import xcmg.device.service.vo.agent.ServiceBorrowOutPrintVO;
import xcmg.device.service.vo.agent.ServiceBorrowOutVO;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * ServiceBorrowOut Controller 实现类
 *
 * @author huhuawei
 */
@RestController
@RequestMapping("/serviceBorrowOut")
public class ServiceBorrowOutController extends BasicController {
    @Autowired
    private ServiceBorrowOutService serviceBorrowOutService;

    @Autowired
    private ServiceBorrowOutCompose serviceBorrowOutCompose;

    @Autowired
    private RepairService repairService;

    @PostMapping(value = "/list")
    @ApiOperation(value = "服务借出单分页列表信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<ServiceBorrowOutVO>> list(@RequestBody SearchCommonVO<ServiceBorrowOutVO> condition) throws Exception {
        condition.getFilters().setOrgId(getOrgId());
        PageCommonVO<ServiceBorrowOutVO> pageCommonVO = serviceBorrowOutService.list(condition);
        collectRepairDOInfo(pageCommonVO.getPageInfoList());
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    //给每个单据附上报修单时候添加的条码信息
    private void collectRepairDOInfo(List<ServiceBorrowOutVO> pageInfoList) {
        List<String> repairCode = new ArrayList<>();
        pageInfoList.forEach(x -> {
            if (StringUtils.isNotBlank(x.getRepairNo())) {
                repairCode.add(x.getRepairNo());
            }
        });
        Map<String, List<RepairDO>> map = repairService.queryMapByCodes(repairCode);
        if (MapUtils.isEmpty(map)) {
            return;
        }
        pageInfoList.forEach(x -> {
            if (StringUtils.isNotBlank(x.getRepairNo())) {
                x.setRepairDOList(map.get(x.getRepairNo()));
            }
        });
    }


    @PostMapping(value = "/save")
    @ApiOperation(value = "服务借出单-保存")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> save(@RequestBody ServiceBorrowOutVO serviceBorrowOutVO) {
        //如果服务借用单 为空  或者借用明细为空
        if (null == serviceBorrowOutVO || serviceBorrowOutVO.getServiceBorrowOutDetailVOS().isEmpty()) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try {
            ErrorCode code = serviceBorrowOutService.save(serviceBorrowOutVO, getUserId(), getUserName(), getOrgId());
            return actionResult(code);
        } catch (Exception e) {
            return new ActionResult<>(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @PostMapping(value = "/syncOrder")
    @ApiOperation(value = "外部同步服务单")
    public ActionResult<Object> syncOrder(@RequestBody ServiceBorrowOutVO serviceBorrowOutVO) throws Exception {
        return actionValueResult(serviceBorrowOutService.syncOrder(serviceBorrowOutVO));
    }


    @GetMapping(value = "/item")
    @ApiOperation(value = "根据id查询服务借出单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBorrowOutVO> item(String id) throws Exception {
        ServiceBorrowOutVO serviceBorrowOutVO = serviceBorrowOutService.queryById(id, getOrgId());
        return actionValueResult(serviceBorrowOutVO);
    }

    @GetMapping(value = "/itemByPrint")
    @ApiOperation(value = "根据id查询服务借出单详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult<ServiceBorrowOutPrintVO> itemByPrint(String id) throws Exception {
        ServiceBorrowOutVO serviceBorrowOutVO = serviceBorrowOutService.queryById(id, getOrgId());
        ServiceBorrowOutPrintVO serviceBorrowOutPrintVO = new ServiceBorrowOutPrintVO();
        serviceBorrowOutPrintVO.setHeaderInfo(serviceBorrowOutVO);
        serviceBorrowOutPrintVO.setDetailList(serviceBorrowOutVO.getServiceBorrowOutDetailVOS());
        return actionValueResult(serviceBorrowOutPrintVO);
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除服务借出单数据")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> delete(@RequestBody List<String> ids) {
        ErrorCode errorCode = null;
        try {
            for (String id : ids) {
                errorCode = serviceBorrowOutService.delete(id, getOrgId());
            }
        } catch (Exception e) {
            return new ActionResult<>(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(errorCode);
    }


    @PostMapping(value = "/manualFinish")
    @ApiOperation("强制完成单据")
    public ActionResult<String> manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception {
        return actionResult(serviceBorrowOutService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
    }

    @PostMapping(value = "/export")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "导出")
    public void export(String[] ids) {
        serviceBorrowOutService.export(Arrays.asList(ids));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "导出全部")
    public void exportAll(ServiceBorrowOutVO condition) throws Exception {
        condition.setOrgId(getOrgId());
        serviceBorrowOutService.exportAll(condition);
    }

    @GetMapping(value = "/outStock")
    @FastMappingInfo(needLogin = true)
    public ActionResult<String> outStock(String id) throws Exception {
        serviceBorrowOutCompose.outStock(id, getUserId(), getOrgId());
        return actionResult(ErrorCode.Success);
    }
}
